#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Attributes:
    __version__ (str): "0.0.1"
    __copyright__ (str): "Copyright (C) 2025 z"
    __license__ (str): "MIT"
    __author__ (str): "Z"
"""
import ssl
import pika

class RabbitMQ:

    def __init__(self, host, port, virtual_host, username, password):
        credentials = pika.PlainCredentials(username, password)
        parameters = []

        # 统一处理所有连接参数组合
        if isinstance(host, list) and isinstance(port, list):
            # 当host和port都是列表时，按索引配对
            for h, p in zip(host, port):
                parameters.append(pika.ConnectionParameters(
                    host=h,
                    port=int(p),
                    heartbeat=300,
                    virtual_host=virtual_host,
                    credentials=credentials
                ))
        elif isinstance(host, list):
            # 当只有host是列表时，使用固定端口
            for h in host:
                parameters.append(pika.ConnectionParameters(
                    host=h,
                    port=int(port),
                    heartbeat=300,
                    virtual_host=virtual_host,
                    credentials=credentials
                ))
        elif isinstance(port, list):
            # 当只有port是列表时，使用固定host
            for p in port:
                parameters.append(pika.ConnectionParameters(
                    host=host,
                    port=int(p),
                    heartbeat=300,
                    virtual_host=virtual_host,
                    credentials=credentials
                ))
        else:
            # 单节点连接
            parameters.append(pika.ConnectionParameters(
                host=host,
                port=int(port),
                heartbeat=300,
                virtual_host=virtual_host,
                credentials=credentials
            ))

        # 移除冗余的单节点连接逻辑
        self.connection = pika.BlockingConnection(parameters)
        self.channel = self.connection.channel()

    # 生产
    def production(self, queue_name):
        # 声明一个 Quorum 队列
        # 注意：RabbitMQ 需要配置支持 quorum queues 的策略，如 'quorum' policy
        self.channel.queue_declare(queue='my_quorum_queue', durable=True, arguments={'x-queue-type': 'quorum'})
        # 现在你可以使用这个队列进行消息的发布和消费了
        # 例如，发布消息：
        self.channel.basic_publish(exchange='', routing_key='my_quorum_queue', body='Hello World!')
        print(" [x] Sent 'Hello World!'")
        # 确保关闭连接
        self.connection.close()


if __name__ == "__main__":

    host = "192.168.2.218"
    port = [5672, 5673, 5674]
    username = "admin"
    password = "123456"
    virtual_host = "customize"

    # 循环发送消息
    for i in range(10):
        rabbitmq = RabbitMQ(host, port, virtual_host, username, password)
        rabbitmq.production("my_quorum_queue")
